//
//  LGTRWZCopyStepCell.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/24.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

//复制数据或者是收集数据
class LGTRWZPostDetailCopyStepCell: UITableViewCell {
    
    var returnStrBlock:ClickCellStrBlock?  //返回给j交互视图
   // var textChangeBlock:ClickReturnStrBlock?  //键盘文字变化回掉处理
    var bottomClickBlock:ClickIndexBlock?
    var titleLabel:UILabel?
    var stepLabel:UILabel?
    var textField:UITextField?
    var stepCellClickBlock:ClickStepPathBlock?
    private var _model:Dictionary<String, Any>?
    var path:IndexPath?
    var copyBtn:UIButton?
    var model:Dictionary<String, Any>{
        get{
            return _model ?? Dictionary.init()
        }set{
            _model = newValue
            self.setData()
        }
    }
    
    func setData(){
        self.titleLabel?.text = self.model["stepText"] as? String ?? ""
        self.stepLabel?.text = "\((self.path?.row ?? 1))"
        let canEdit:Bool = self.model["canEdit"] as? Bool ?? false
        self.textField?.isUserInteractionEnabled = false
        
        let copy = self.model["copy"] as? Bool ?? false
        self.textField?.text = self.model["submitInfo"] as? String ?? ""
        if copy == true{
             self.textField?.text = self.model["subText"] as? String ?? ""
        }else{
            self.textField?.isUserInteractionEnabled = canEdit
        }
        self.copyBtn?.isHidden = !copy
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        //self.isUserInteractionEnabled = false
        
        self.titleLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(15), text: "扫描下方二维码")
        self.titleLabel?.numberOfLines = 0
        self.addSubview(self.titleLabel!)
        self.titleLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFitFloat(41))
            make.right.equalTo(-SizeFitFloat(48))
            make.top.equalTo(SizeFit(22))
        }
        
        self.stepLabel =  LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: UIColor.white, fontSize: SizeFitFloat(16), text: "1", corner: SizeFitFloat(10))
        self.addSubview(self.stepLabel!)
        self.stepLabel?.backgroundColor = YellowColor
        self.stepLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.height.width.height.equalTo(SizeFit(20))
            make.centerY.equalTo(self.titleLabel!)
        }
        
        //请按要求输入文字内容
        self.textField = LGTViewCreateUtil.share.createTextFied(font: Font_Normal_15, placeholder:"请按要求输入文字内容", textColor: TextColor)
        self.textField?.layer.masksToBounds = true
        self.textField?.layer.cornerRadius = SizeFitFloat(5)
        self.textField?.layer.borderWidth = 1
        //self.textField?.layer.borderColor = YellowColor.cgColor
        self.textField?.layer.borderColor = AllLineColor.cgColor
        self.addSubview(self.textField!)
        let backTop = SizeFit(18)
        self.textField?.snp.makeConstraints({ (make) in
            make.left.equalTo(SizeFit(42))
            make.height.equalTo(SizeFit(31))
            make.width.equalTo(SizeFit(218))
            //make.top.equalTo(SizeFit(55))
            //make.top.equalTo(self.titleLabel!.snp_bottomMargin).offset(SizeFit(18))
            
            make.top.equalTo(self.self.titleLabel!.safeAreaLayoutGuide.snp.bottom).offset(backTop)
            if #available(iOS 11.0, *){
                make.top.equalTo(self.self.titleLabel!.safeAreaLayoutGuide.snp.bottom).offset(backTop)
            }else{
                make.top.equalTo(self.self.titleLabel!.snp_bottomMargin).offset(backTop)
            }
        })
        self.textField?.leftViewMode =  UITextField.ViewMode.always
        self.textField!.leftView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: SizeFit(10), height: 10.0))
        //self.textField!.leftView!.backgroundColor = UIColor.orange
        self.textField?.isUserInteractionEnabled = false
        
        self.copyBtn = LGTViewCreateUtil.share.createTextLGTButton(title: "复制数据", font: Font_Normal_15, color: UIColor.white, selectedColor: UIColor.white, dW: 0, dH: 0, corner: SizeFitFloat(5), backColor: YellowColor)
        self.copyBtn?.tag = StepCellClickType.copydata.rawValue
        self.copyBtn?.addTarget(self, action: #selector(copyAction), for: UIControl.Event.touchUpInside)
        
        self.addSubview(self.copyBtn!)

        self.copyBtn!.snp.makeConstraints { (make) in
            make.width.equalTo(SizeFit(97))
            make.height.equalTo(SizeFit(31))
            make.centerY.equalTo(self.textField!)
            make.right.equalTo(SizeFit(-11))
        }
        
        let imageLine:UIImageView = LGTViewCreateUtil.share.createLineView()
        self.addSubview(imageLine)
        imageLine.backgroundColor = ColorUtil(color: 0xEFEFEF)
        imageLine.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.right.equalTo(-SizeFit(11))
            make.height.equalTo(1)
            make.top.equalTo(self.textField!.snp_bottomMargin).offset(SizeFit(22))
            make.bottom.equalTo(0)
        }
        
        //值变化回掉
        self.textField?.addTarget(self, action: #selector(dataChangeAction), for: .editingDidEnd)
    }
    
    @objc func dataChangeAction(){
        if self.returnStrBlock != nil{
            self.returnStrBlock!(self.textField?.text ?? "", self.path!)
        }
    }
    
    func gotoActionBlock(index:Int){
        if self.stepCellClickBlock != nil{
            self.stepCellClickBlock!(self.model, self.path!, index, nil)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func copyAction(){
        if self.stepCellClickBlock != nil{
            self.stepCellClickBlock!(self.model, self.path!, StepCellClickType.copydata.rawValue, nil)
        }
    }
}

